package com.lan.java8.model.info;

import com.lan.java8.antlr.Java8Parser;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

@Data
public class ClassInfo {

    private String var = "class";

    private String name;

    private List<Field> fields;

    private List<MethodInfo> methodInfos;

    private String superclassName;

    private List<String> superInterfaceNames;

    public void start(Java8Parser.NormalClassDeclarationContext context) {
        Java8Parser.ClassNameContext classNameContext = context.className();
        if (classNameContext != null) {
            this.name = classNameContext.getText();
        }
        Java8Parser.SuperclassContext superclassContext = context.superclass();
        if (superclassContext != null) {
            superclassContext(superclassContext);
        }
        Java8Parser.SuperinterfacesContext superinterfacesContext = context.superinterfaces();
        if (superinterfacesContext != null) {
            superinterfacesContext(superinterfacesContext);
        }

        Java8Parser.ClassBodyContext classBodyContext = context.classBody();
        if (classBodyContext != null) {
            classBodyContext.classBodyDeclaration();
            classBodyContext(classBodyContext);
        }
    }


    private void classBodyContext(Java8Parser.ClassBodyContext classBodyContext) {
        List<Java8Parser.ClassBodyDeclarationContext> bodyDeclarationContextList = classBodyContext.classBodyDeclaration();
        if (bodyDeclarationContextList != null && bodyDeclarationContextList.size() > 0) {
            for (int i = 0; i < bodyDeclarationContextList.size(); i++) {
                Java8Parser.ClassBodyDeclarationContext classBodyDeclarationContext = bodyDeclarationContextList.get(i);
                Java8Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classBodyDeclarationContext.classMemberDeclaration();
                if (classMemberDeclarationContext != null) {
                    Java8Parser.FieldDeclarationContext fieldDeclarationContext = classMemberDeclarationContext.fieldDeclaration();
                    if (fieldDeclarationContext != null) {
                        List<Field> fieldList = Field.get(fieldDeclarationContext);
                        if (fieldList != null) {
                            this.fields.addAll(fieldList);
                        }
                    }
                    Java8Parser.MethodDeclarationContext methodDeclarationContext = classMemberDeclarationContext.methodDeclaration();
                    if (methodDeclarationContext != null) {
                        MethodInfo methodInfo = new MethodInfo();
                        methodInfo.get(methodDeclarationContext);
                        if (methodInfo.isIs()) {
                            this.methodInfos.add(methodInfo);
                        }
                    }
                }
            }
        }
    }


    private void classBodyDeclarationContext(Java8Parser.ClassBodyDeclarationContext classBodyDeclarationContext) {
        classBodyDeclarationContext.classMemberDeclaration();
    }

    private void superinterfacesContext(Java8Parser.SuperinterfacesContext superinterfacesContext) {
        Java8Parser.InterfaceTypeListContext InterfaceTypeListContext = superinterfacesContext.interfaceTypeList();
        if (InterfaceTypeListContext != null) {
            List<Java8Parser.InterfaceTypeContext> interfaceTypeList = InterfaceTypeListContext.interfaceType();
            if (interfaceTypeList != null && interfaceTypeList.size() > 0) {
                for (int i = 0; i < interfaceTypeList.size(); i++) {
                    Java8Parser.InterfaceTypeContext o = interfaceTypeList.get(i);
                    interfaceTypeContext(o);
                }
            }
        }
    }

    private void interfaceTypeContext(Java8Parser.InterfaceTypeContext context) {
        this.superInterfaceNames.add(context.getText());
    }


    private void superclassContext(Java8Parser.SuperclassContext superclassContext) {
        String txt = superclassContext.getText();
        if (StringUtils.isNotBlank(txt)) {
            this.superclassName = txt;
        }
    }
}
